#!/bin/bash

#sudo cp /etc/hosts /etc/hosts.bak
#sudo cp /etc/hosts.raw /etc/hosts
#sudo sed -i "" "/# GitHub520 Host Start/,/# Github520 Host End/d" /etc/hosts && curl https://raw.hellogithub.com/hosts | sudo tee -a /etc/hosts

help() {
	echo "Command: ./github520.sh refresh|restore"
	echo -e "SubCommand \t Infomation"
	echo -e "refresh \t reset Github520 mapping to /etc/hosts"
	echo -e "restore \t restore /etc/hosts of raw"
}

check_raw() {
	if [ -f "/etc/hosts.raw" ]; then
		echo "/etc/hosts.raw already exists"
	else
		echo "backup /etc/hosts to /etc/hosts.raw"
		sudo cp /etc/hosts /etc/hosts.raw
	fi
}

if [ $# -lt 1 ]; then
	help
	exit 1
fi

case "$1" in
"refresh")
	check_raw
	sudo cp /etc/hosts.raw /etc/hosts
	sudo sed -i "" "/# GitHub520 Host Start/,/# Github520 Host End/d" /etc/hosts && curl https://raw.hellogithub.com/hosts | sudo tee -a /etc/hosts
	;;
"restore")
	sudo cp /etc/hosts.raw /etc/hosts
	sudo rm -rf /etc/hosts.raw
	;;
*)
	help
	;;
esac
